const { getOrders, addOrders, deleteOrders, getDetails } = require("../dao/ordersDao");
// 获取订单
module.exports.getOrders = async function ({ currentPage, pageSize, value, input, state }) {
    var data = await getOrders({ currentPage, pageSize, value, input, state });
    if (data.total / pageSize + 1 < currentPage) {
        currentPage = 1;
        var data = await getOrders({ currentPage, pageSize, value, input, state });
    }
    return {
        message: "数据获取成功",
        status: 1,
        data
    }
}


// 新增订单
module.exports.addOrders = async function (newOrders) {
    // 调用第三层的方法，将数据传输过去
    const result = await addOrders(newOrders);
    if (result._id) {
        return {
            msg: '新增成功',
            status: 1
        }
    } else {
        return {
            msg: '新增失败',
            status: 0
        }
    }
}

// 删除订单
module.exports.deleteOrders = async function (_id) {
    const result = await deleteOrders(_id);
    // 如果当删除数大于0时删除成功
    if (result.deletedCount > 0) {
        return {
            message: '删除成功',
            status: 1
        }
    } else {
        return {
            message: '删除失败',
            status: 0
        }
    }
}

// 查看详情
module.exports.getDetails = async function (_id) {
    const result = await getDetails(_id);
    return {
        message: "数据获取成功",
        status: 1,
        data: result
    }
}